//
//  ICRedPacketResultHeaderView.m
//  ICome
//
//  Created by 席鹏飞 on 2016/11/11.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICRedPacketResultHeaderView.h"
#import "ICClickRedPacketListModel.h"
#import "ICHeadImageView.h"
#import "NSMutableAttributedString+Extension.h"
#import "NSString+YYAdd.h"
#import "UIImage+AvatarGeneration.h"
#import "ICHeaderFile.h"

@interface ICRedPacketResultHeaderView()
@property (nonatomic, strong) UIImage *redPacketImage;
@property (nonatomic, strong) ICHeadImageView *headerImageView;  // 头像
@property (nonatomic, strong) UIButton *nameButton; // 姓名
@property (nonatomic, strong) UILabel *messageLabel; // 留言
@property (nonatomic, strong) UILabel *workPointLabel; // 工分
@property (nonatomic, strong) UILabel *promptLabel; // 底部提示
@property (nonatomic, strong) UIButton *seeMyPacketButton;  // 查看我的红包
@property (nonatomic, strong) UIImageView *bottomLineImage; // 底部线
@end

@implementation ICRedPacketResultHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.redPacketImage = [UIImage imageNamedFromiComeSDKBundle:@"icon_my_redpacket_gray"];
    }
    
    return self;
}

- (void)layoutSubview:(ICClickRedPacketModel *)model {
    NSString *pageNo = model.pageNo;
    
    [self.headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(self.mas_top).offset(15);
        make.width.mas_equalTo(45);
        make.height.mas_equalTo(45);
    }];
    [self.nameButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(self.headerImageView.mas_bottom).offset(5);
        make.height.mas_equalTo(20);
    }];
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(self.nameButton.mas_bottom).offset(5);
        make.height.mas_equalTo(16);
    }];

    if ([pageNo intValue] != 24 && [pageNo intValue] != 25 && [pageNo intValue] != 14) {
        [self.promptLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.mas_centerX);
            make.bottom.equalTo(self.mas_bottom).offset(-20);
            make.height.mas_equalTo(16);
        }];
        [self.workPointLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.mas_centerX);
            make.top.equalTo(self.messageLabel.mas_bottom).offset(25);
            make.bottom.equalTo(self.promptLabel.mas_top).offset(-20);
        }];
        CGFloat mrWidth = [@"“我的红包”" sizeForFont:[UIFont systemFontOfSize:12] size:CGSizeMake(MAXFLOAT,MAXFLOAT) mode:NSLineBreakByWordWrapping].width;
        [self.seeMyPacketButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.promptLabel.mas_top).offset(-10);
            make.bottom.equalTo(self.promptLabel.mas_bottom).offset(10);
            make.right.equalTo(self.promptLabel.mas_right);
            make.width.mas_equalTo(mrWidth);
        }];
    }
    
    [self.bottomLineImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.bottom.equalTo(self.mas_bottom);
        make.height.mas_equalTo(0.5);
    }];
}

- (void)setModel:(ICClickRedPacketModel *)model {
    [self layoutSubview:model];
    NSString *pageNo = model.pageNo;
    
    NSString *URL = [ICTools getOglImageURL:([ICMessageDatabase getPhotoIdWithEId:model.sender])];
    [UIImage addAvatariamgeView:self.headerImageView.imageView withUrlStr:URL witheId:model.sender withName:[ICMessageDatabase getUserNameWithEId:model.sender]];
    
//    [self.headerImageView.imageView sd_setImageWithURL:[NSURL URLWithString:OGLIMAGEURL([ICMessageDatabase getPhotoIdWithEId:model.sender])] placeholderImage:[UIImage imageNamed:@"App_personal_headimg"]];
    NSString *name = [NSString stringWithFormat:@"%@的红包", [ICMessageDatabase getUserNameWithEId:model.sender]];
    [self.nameButton setTitle:name forState:UIControlStateNormal] ;
    CGFloat nWidth = [name sizeForFont:[UIFont systemFontOfSize:15] size:CGSizeMake(MAXFLOAT,MAXFLOAT) mode:NSLineBreakByWordWrapping].width+2;
    CGFloat iWidth = self.redPacketImage.size.width;
    self.nameButton.titleEdgeInsets = UIEdgeInsetsMake(0, -iWidth, 0, iWidth);
    self.nameButton.imageEdgeInsets = UIEdgeInsetsMake(0, nWidth, 0, -nWidth);
    if (model.message.length > 0) {
        self.messageLabel.text = model.message;
    }else {
        self.messageLabel.text = @"节节高升，再创佳绩!";
    }
    
    if ([pageNo intValue] != 24 && [pageNo intValue] != 25 && [pageNo intValue] != 14) {
        if (model.amount.length == 0) {
            model.amount = @"0";
        }
        NSMutableAttributedString *workPoint = [NSMutableAttributedString attributedString:@[model.amount, @"工分"] colors:@[ICRGB(0xe44e4e), ICRGB(0xe44e4e)] fonts:@[ICFont(50.0 * Level), [UIFont systemFontOfSize:15]]];
        self.workPointLabel.attributedText = workPoint;
        NSMutableAttributedString *prompt = [NSMutableAttributedString attributedString:@[@"已存入工分账户，点击可查看", @"“我的红包”"] colors:@[ICRGB(0x999999), ICRGB(0x759cde)] fonts:@[[UIFont systemFontOfSize:12], [UIFont systemFontOfSize:12]]];
        self.promptLabel.attributedText = prompt;
    }
}

#pragma mark - event response

- (void)seeMyPacketBtnClicked:(id)sender {
    if ([self.delegate respondsToSelector:@selector(myPacketBtnClicked:)]) {
        [self.delegate myPacketBtnClicked:self];
    }
}

#pragma mark - Getter

- (ICHeadImageView *)headerImageView {
    if (!_headerImageView) {
        _headerImageView = [[ICHeadImageView alloc] init];
        [self addSubview:_headerImageView];
    }
    return _headerImageView;
}

- (UIButton *)nameButton {
    if (!_nameButton) {
        _nameButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_nameButton setTitleColor:ICRGB(0x696363) forState:UIControlStateNormal];
        [_nameButton setImage:self.redPacketImage forState:UIControlStateNormal];
        [_nameButton setImage:self.redPacketImage forState:UIControlStateHighlighted];
        _nameButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [self addSubview:_nameButton];
    }
    return _nameButton;
}

- (UILabel *)messageLabel {
    if (!_messageLabel) {
        _messageLabel = [[UILabel alloc] init];
        _messageLabel.textColor = ICRGB(0x999999);
        _messageLabel.font = ICFont(14.0 * Level);
        [self addSubview:_messageLabel];
    }
    return _messageLabel;
}

- (UILabel *)workPointLabel {
    if (!_workPointLabel) {
        _workPointLabel = [[UILabel alloc] init];
        _workPointLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_workPointLabel];
    }
    return _workPointLabel;
}

- (UILabel *)promptLabel {
    if (!_promptLabel) {
        _promptLabel = [[UILabel alloc] init];
        [self addSubview:_promptLabel];
    }
    return _promptLabel;
}

- (UIButton *)seeMyPacketButton {
    if (!_seeMyPacketButton) {
        _seeMyPacketButton = [[UIButton alloc] init];
        [_seeMyPacketButton addTarget:self action:@selector(seeMyPacketBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_seeMyPacketButton];
    }
    return _seeMyPacketButton;
}

- (UIImageView *)bottomLineImage {
    if (!_bottomLineImage) {
        _bottomLineImage = [[UIImageView alloc] init];
        _bottomLineImage.backgroundColor = LINEBACKGROUNDCOLOR;
        [self addSubview:_bottomLineImage];
    }
    return _bottomLineImage;
}

@end
